<?php

class odcf_myForm_Element_Popover extends Zend_Form_Element {

    private static $_placementData = array(
        'top'  => true, 'bottom' => true, 'left' => true, 'right' => true
    );
    
    private static $_triggerData = array(
        'click'  => true, 'hover' => true, 'focus' => true, 'manual' => true
    );
    
    private $_jsWrite = false;
    /**
     * Apply a css fade transition to the tooltip
     * @var boolean
    */
    protected $_animation = true;
    
    /**
     * Insert html into the popover.
     * If false, jquery's text method will be used to insert content into the dom.
     * Use text if you're worried about XSS attacks.
     * @var boolean
    */
    protected $_html = false;
    
    /**
     * How to position the popover - top | bottom | left | right
     * @var string
    */
    protected $_placement = 'right'; 
    
    /**
     * How popover is triggered - click | hover | focus | manual
     * @var string
    */
    protected $_trigger = 'click'; 
    
    /**
     * default title value if `title` attribute isn't present
     * @var string
    */
    protected $_title = ''; 
    
    /**
     * Default content value if `data-content` attribute isn't present
     * @var string
    */
    protected $_content = ''; 
    
    /**
     * Delay showing and hiding the popover (ms) - does not apply to manual trigger type
     * If a number is supplied, delay is applied to both hide/show
     * Object structure is: delay: { show: 500, hide: 100 }
     * @var int or object
    */
    protected $_delay = 0; 
    
    /**
     * Appends the popover to a specific element container: 'body'
     * @var string
    */
    protected $_container = 'false'; 
    
    /**
     * Set animation flag
     *
     * @param  boolean $flag
     * @return My_Form_Element_Popover
     */
    public function setAnimation($flag){

        $this->_animation = (bool) $flag;
        return $this;
    }
    
    public function getAnimation(){
        return $this->_animation;
    }

    /**
     * Set html flag
     *
     * @param  boolean $flag
     * @return My_Form_Element_Popover
     */
    public function setHtml($flag){

        $this->_html = (bool) $flag;
        return $this;
    }
    
    public function getHtml(){
        return $this->_html;
    }
    
    /**
     * Set placement
     *
     * @param  string top|bottom|left|right
     * @return My_Form_Element_Popover
     */
    public function setPlacement($placement){

        if (isset(self::$_placementData[(string) $placement]) === true) {
            $this->_placement = $placement;
        }
        return $this;
    }
    
    public function getPlacement(){
        return $this->_placement;
    }
    
    /**
     * Set trigger
     *
     * @param  string click|hover|focus|manual
     * @return My_Form_Element_Popover
     */
    public function setTrigger($trigger){

        if (isset(self::$_triggerData[(string) $trigger]) === true) {
            $this->_trigger = $trigger;
        }
        return $this;
    }
    
    public function getTrigger(){
        return $this->_trigger;
    }
    
    /**
     * Set popover title
     *
     * @param  string $title
     * @return My_Form_Element_Popover
     */
    public function setTitle($title)
    {
        $this->_title = (string) $title;
        return $this;
    }
    
    public function getTitle(){
        return $this->_title;
    }
    
    /**
     * Set popover content
     *
     * @param  string $content
     * @return My_Form_Element_Popover
     */
    public function setContent($content)
    {
        $this->_content = (string) $content;
        return $this;
    }
    
    public function getContent(){
        return $this->_content;
    }
    
    /**
     * Set popover delay
     *
     * @param  string|object $delay
     * @return My_Form_Element_Popover
     */
    public function setDelay($delay)
    {
        if(is_int($delay)){
            $this->_delay = (int) $delay;
        }else{
            $this->_delay = $delay;
        }
        
        return $this;
    }
    
    public function getDelay(){
        return $this->_delay;
    }
    
    /**
     * Set popover container
     *
     * @param  string $container
     * @return My_Form_Element_Popover
     */
    public function setContainer($container)
    {
        $this->_container = (string) $container;
        return $this;
    }
    
    public function getContainer(){
        return $this->_container;
    }
    
    public function __construct($spec, $options = null)
    {
        parent::__construct($spec, $options);
        
        $script = '$("a[data-toggle=popover]").popover().click(function(e) {e.preventDefault()});';
        $this->getView()->inlineScript()->setScript($script, $type = 'text/javascript', array());
        $this->_jsWrite = false;
    }
    
    public function render(Zend_View_Interface $view = null){
        
        if (null !== $view) {
            $this->setView($view);
        }
        
        $animation = 'data-animation="'.$this->getAnimation().'"';
        $html = 'data-html="'.$this->getHtml().'"';
        $placement = 'data-placement="'.$this->getPlacement().'"';
        $trigger = 'data-trigger="'.$this->getTrigger().'"';
        $title = 'data-title="'.$this->getTitle().'"';
        $contenu = 'data-content="'.$this->getContent().'"';
        $delay = 'data-delay="'.$this->getDelay().'"';
        $container = 'data-container="'.$this->getContainer().'"';
        
        $markup = '<a title="" href="#" data-toggle="popover" ' . $contenu . ' ' . $placement . ' ' . $trigger
                . ' ' . $title . ' ' . $delay . ' ' . $animation . ' ' . $html  . ' '
                . $container .'><i class="icon-info-sign"></i></a>';
        if(!$this->_jsWrite){
            $markup .= $this->getView()->inlineScript();
            $this->_jsWrite = true;
        }
        return $markup;
    }
    
    
}
